/**
 * @version 1.0.0
 * @author fxg1997
*/
<template>
  <div class="pages_content table">
    <el-container>
      <el-header>
        <breaDcrumb />
      </el-header>
      <el-main style="display: flex">
        <div>
          <div class="header_right" @click="showCardNav = !showCardNav">
            <div class="title">搜索</div>
            <div>
              <transition name="from"></transition>
              <span v-show="!showCardNav">
                <i style="margin-right: 5px" class="el-icon-arrow-down"></i>
              </span>
              <transition name="to"></transition>
              <span v-show="showCardNav">
                <i style="margin-right: 5px" class="el-icon-arrow-up"></i>
              </span>
            </div>
          </div>
          <div v-show="showCardNav" class="hr"></div>
          <el-collapse-transition>
            <div v-show="showCardNav">
              <div class="search">
                <span style="display: inline-block;margin-bottom: 10px;">
                  <span style="margin-right: 12px">名称</span>
                  <el-input v-model="tableParam.name" style="width: 240px" clearable placeholder="请输入" @keyup.enter.native="gettableData()" />
                </span>
                <span style="margin-left: 24px">
                  <span style="margin-right: 12px">规格</span>
                  <el-input v-model="tableParam.specificationName" style="width: 240px" clearable placeholder="请输入" @keyup.enter.native="gettableData()" />
                </span>
                <span style="margin-left: 24px">
                  <span style="margin-right: 12px">类型</span>
                  <el-input v-model="tableParam.type" style="width: 240px" clearable placeholder="请输入" @keyup.enter.native="gettableData()" />
                </span>
                <el-button icon="el-icon-search" size="medium" class="bottom_all" @click="(tableParam.page = 1), gettableData()">查 询</el-button>
                <el-button size="medium" class="bottom_all_two" style="margin-left: 12px" @click="init()">重 置</el-button>
              </div>
            </div>
          </el-collapse-transition>
          <div class="hr"></div>
          <div class="card">
            <div class="card_nav">
              <div>
                <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='TIANJIA'))!==-1" size="small" class="bottom_all" style="margin-left: 0" @click="addFromDate('添加')">
                  <i class="icon iconfont iconicon_button_add" style="font-size: 12px"></i>
                  添加周材
                </el-button>
                <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='ZCPD'))!==-1" style="margin-left:12px" size="small" class="bottom_all_two" @click="open('dialog1')">
                  周材盘点
                </el-button>
                <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='ZCTJ'))!==-1" size="small" class="bottom_all_two" style="margin-left: 12px" @click="open('dialog2')">
                  周材调级
                </el-button>
                <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='PDJL'))!==-1" size="small" class="bottom_all_two" style="margin-left:  12px" @click="open('dialog3')">
                  盘点记录
                </el-button>
              </div>
              <div>
                <el-dropdown :hide-on-click="false" trigger="click" style="margin: 0 20px">
                  <el-link :underline="false">
                    <span class="el-icon--left">
                      <i class="iconfont iconicon_button_filtrate"></i>
                    </span>
                    筛选
                  </el-link>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item v-for="(item, index) in table" :key="index">
                      <el-checkbox v-model="item.status">{{
                        item.label
                        }}</el-checkbox>
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </div>
            </div>
            <div v-loading="tableLoading" class="card_info" :style="style">
              <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width: 100%" :data="tableData" tooltip-effect="dark">
                <template v-for="(item, index2) in table">
                  <el-table-column v-if="item.status" :key="index2" :label="item.label" align="center" :min-width="item.width">
                    <template slot-scope="scope">
                      <span v-if="item.label == '在库数量'">
                        {{ scope.row.repertoryAmount - scope.row.stockOutAmount}}
                      </span>
                      <span v-else>{{ scope.row[item.prop] }}</span>
                    </template>
                  </el-table-column>
                </template>
              </el-table>
            </div>
            <!-- <div class="card_bottom">
              <div class="card_bottom_left">
                <div class="top_page_totle">总共{{ total }}条</div>
                <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" layout="prev, pager, next, jumper" :total="total" @current-change="gettableData()"></el-pagination>
              </div>
              <div>
                <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="gettableData">刷新</el-button>
              </div>
            </div> -->
          </div>
        </div>
      </el-main>
      <div class="card_bottom">
        <div class="card_bottom_left">
          <div class="top_page_totle">总共{{ total }}条</div>
          <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" layout="prev, pager, next, jumper" :total="total" @current-change="gettableData()"></el-pagination>
        </div>
        <div>
          <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="gettableData">刷新</el-button>
        </div>
      </div>
    </el-container>
    <!-- 添加 -->
    <el-dialog v-dialogDrag :visible.sync="addDialog" width="480px" :close-on-click-modal="false" :before-close="resetFromDate">
      <span slot="title" class="dialog-title">{{ addDialogTitle }}</span>
      <div class="dialoginfo">
        <el-form ref="reffromDate" :rules="rules" :inline="true" :model="fromDate" label-width="110px">
          <el-form-item prop="name">
            <span slot="label" style="color: #ff0000">名称</span>
            <el-input v-model="fromDate.name" placeholder="请输入" clearable>
            </el-input>
          </el-form-item>
          <el-form-item prop="specificationId">
            <span slot="label" style="color: #ff0000">规格</span>
            <el-select ref="specification" v-model="fromDate.specificationId" placeholder="请选择" @change="$nextTick(()=>{fromDate.specificationName = $refs.specification.selectedLabel})">
              <el-option v-for="(item, index) in zhoucaispecificationList" :key="index" :label="item.itemName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="typeId">
            <span slot="label" style="color: #ff0000">类型</span>
            <el-select ref="type" v-model="fromDate.typeId" placeholder="请选择" @change="$nextTick(()=>{fromDate.type = $refs.type.selectedLabel})">
              <el-option v-for="(item, index) in zhoucaiTypeList" :key="index" :label="item.itemName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="repertoryAmount">
            <span slot="label" style="color: #ff0000">数量</span>
            <el-input v-model="fromDate.repertoryAmount" placeholder="请输入" onkeypress='return( /\d/.test(String.fromCharCode(event.keyCode)))' @input="fromDate.repertoryAmount=$regExp.checkNum(fromDate.repertoryAmount)" @blur="fromDate.repertoryAmount=$regExp.blurCheckNum(fromDate.repertoryAmount)">
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="resetFromDate">取 消</el-button>
        <el-button class="bottom_all" style="margin-left: 24px" @click="pagesubmitFromDate()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 周材盘点 -->
    <el-dialog v-dialogDrag :visible.sync="dialog1" width="480px" :close-on-click-modal="false">
      <span slot="title" class="dialog-title">周材盘点</span>
      <div class="dialoginfo">
        <el-form v-if="dialog1" ref="dialog1" :rules="rules1" :inline="true" :model="lowData" label-width="110px">
          <el-form-item prop="name">
            <span slot="label" style="color: #ff0000">名称</span>
            <el-select v-model="lowData.name" placeholder="请选择" @change="change(lowData.name)">
              <el-option v-for="(item, index) in nameList" :key="index" :value="item"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="specificationId">
            <span slot="label" style="color: #ff0000">规格</span>
            <el-select ref="specification1" v-model="lowData.specificationId" placeholder="请选择" :disabled="specificationList.length==0" @change="change1(lowData.specificationId)">
              <el-option v-for="(item, index) in specificationList" :key="index" :label="item.specificationName" :value="item.specificationId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="typeId">
            <span slot="label" style="color: #ff0000">等级/类型</span>
            <el-select ref='.type1' v-model="lowData.typeId" placeholder="请选择" :disabled="typeList.length==0" @change="change2(lowData.typeId)">
              <el-option v-for="(item, index) in typeList" :key="index" :label="item.type" :value="item.typeId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="checkAmount">
            <span slot="label" style="color: #ff0000">盘点数量</span>
            <el-input v-model="lowData.checkAmount" placeholder="请输入" onkeypress='return( /\d/.test(String.fromCharCode(event.keyCode)))' @input="lowData.checkAmount=$regExp.checkNum(lowData.checkAmount)" @blur="lowData.checkAmount=$regExp.blurCheckNum(lowData.checkAmount)">
            </el-input>
          </el-form-item>
          <div>
            <el-form-item>
              <span slot="label" style="color: #ff0000">差额</span>
              {{ Number(lowData.repertoryAmount) - Number(lowData.checkAmount)||''}}
            </el-form-item>
          </div>
          <el-form-item prop="code">
            <span slot="label" style="color: #ff0000">备注</span>
            <el-input v-model="lowData.remark" placeholder="请填写备注" class="textarea" type="textarea" resize="none" rows="5"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="reset('dialog1')">取 消</el-button>
        <el-button class="bottom_all" style="margin-left: 24px" @click="submit1()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 周材调级 -->
    <el-dialog v-dialogDrag :visible.sync="dialog2" width="480px" :close-on-click-modal="false">
      <span slot="title" class="dialog-title">周材调级</span>
      <div class="dialoginfo">
        <el-form v-if="dialog2" ref="dialog2" :rules="rules2" :inline="true" :model="lowData" label-width="110px">
          <el-form-item prop="name">
            <span slot="label" style="color: #ff0000">名称</span>
            <el-select v-model="lowData.name" placeholder="请选择" @change="change3(lowData.name)">
              <el-option v-for="(item, index) in nameList" :key="index" :value="item"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="specificationId">
            <span slot="label" style="color: #ff0000">规格</span>
            <el-select v-model="lowData.specificationId" placeholder="请选择" :disabled="specificationList.length==0" @change="change4(lowData.specificationId)">
              <el-option v-for="(item, index) in specificationList" :key="index" :label="item.specificationName" :value="item.specificationId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="typeId2">
            <span slot="label" style="color: #ff0000">当前等级</span>
            <el-select v-model="lowData.typeId2" placeholder="请选择" :disabled="typeList.length==0" @change="change5(lowData.typeId2)">
              <el-option v-for="(item, index) in typeList" :key="index" :label="item.type" :value="item.typeId"></el-option>
            </el-select>
          </el-form-item>
          <div>
            <el-form-item>
              <span slot="label" style="color: #ff0000">在库数量</span>
              <div>{{lowData.repertoryAmount2}}</div>
            </el-form-item>
          </div>
          <el-form-item prop="typeId">
            <span slot="label" style="color: #ff0000">调整等级</span>
            <el-select ref="type" v-model="lowData.typeId" placeholder="请选择" @change="$nextTick(()=>{lowData.type = $refs.type.selectedLabel})">
              <el-option v-for="(item, index) in zhoucaiTypeList" :key="index" :label="item.itemName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="repertoryAmount">
            <span slot="label" style="color: #ff0000">调整数量</span>
            <el-input v-model="lowData.repertoryAmount" type="Number" placeholder="请输入" clearable @input="lowData.repertoryAmount>lowData.repertoryAmount2?lowData.repertoryAmount=lowData.repertoryAmount2:''">
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="reset('dialog2')">取 消</el-button>
        <el-button class="bottom_all" style="margin-left: 24px" @click="submit2()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 盘点记录 -->
    <el-dialog v-dialogDrag :visible.sync="dialog3" width="1180px" :close-on-click-modal="false">
      <span slot="title" class="dialog-title">盘点记录</span>
      <div>
        <div style="padding-bottom:20px">
          <span style="margin-left: 24px">
            <span style="margin-right: 12px">盘点时间</span>
            <el-date-picker v-model="value1" type="datetimerange" unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </span>
          <el-button icon="el-icon-search" size="mini" class="bottom_all" @click="queryList1(value1)">查 询</el-button>
        </div>
        <el-table v-if="dialog3" :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width: 100%" :data="lowData" tooltip-effect="dark">
          <el-table-column label="序号" align="center" prop="name">
            <template slot-scope="scope">
              {{scope.$index+1}}
            </template>
          </el-table-column>
          <el-table-column label="名称" align="center" prop="name"></el-table-column>
          <el-table-column label="规格" align="center" prop="specificationName"></el-table-column>
          <el-table-column label="等级" align="center" prop="type"></el-table-column>
          <el-table-column label="库存数量" align="center" prop="repertoryAmount"></el-table-column>
          <el-table-column label="盘点数量" align="center" prop="checkAmount"></el-table-column>
          <el-table-column label="差额" align="center" prop="">
            <template slot-scope="scope">
              {{ Number(scope.row.repertoryAmount) - Number(scope.row.checkAmount)||0}}
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center" prop="remark"></el-table-column>
          <el-table-column label="盘点时间" align="center" prop="createTime" width="160"></el-table-column>
          <el-table-column label="盘点人员" align="center" prop="createUserName"></el-table-column>
        </el-table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="reset('dialog3')">取 消</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import mixin from "@/mixins/ICPSmixin.js";
import {
  queryPage,
  saveOrUpdate,
  queryNameList,
  querySpecification,
  saveOneResord,
  queryList,
  updateOrSave
} from "@/api/besSer/WMmanagement/inventory.js";
import { sortReg } from "@/common/RegExp.js";
import { getDicsType } from "@/api/common.js";
import { getUserInfo } from "@/utils/auth"
export default {
  mixins: [mixin],
  data() {
    var validatePass1 = (rule, value, callback) => {
      if (sortReg(value)) {
        callback();
      } else {
        callback(new Error("请输正整数!"));
      }
    };
    return {
      /*表格增删改查及下载接口*/
      queryPage: queryPage,
      saveOrUpdate: saveOrUpdate,
      /*表格数据*/
      table: [
        { label: "编码", width: "120", prop: "code", status: true },
        { label: "名称", width: "120", prop: "name", status: true },
        { label: "规格", width: "auto", prop: "specificationName", status: true },
        { label: "等级/类型", width: "120", prop: "type", status: true },
        { label: "库存总量", width: "auto", prop: "repertoryAmount", status: true },
        { label: "出库数量", width: "120", prop: "stockOutAmount", status: true },
        { label: "在库数量", width: "auto", prop: "", status: true },
      ],
      pagesType: 0,
      /*添加表单数据*/
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        specificationId: [{ required: true, message: "请选择规格", trigger: "change" }],
        typeId: [{ required: true, message: "请选择类型", trigger: "change" }],
        repertoryAmount: [{ required: true, message: "请输入数量", trigger: "blur" },
        { validator: validatePass1, trigger: "blur", }
        ],
      },
      rules1: {
        name: [{ required: true, message: "请选择名称", trigger: "change" }],
        specificationId: [{ required: true, message: "请选择规格", trigger: "change" }],
        typeId: [{ required: true, message: "请选择类型", trigger: "change" }],
        checkAmount: [{ required: true, message: "请输入盘点数量", trigger: "blur" },
        { validator: validatePass1, trigger: "blur", }
        ],
      },
      rules2: {
        name: [{ required: true, message: "请选择名称", trigger: "change" }],
        specificationId: [{ required: true, message: "请选择规格", trigger: "change" }],
        typeId: [{ required: true, message: "请选择当前等级", trigger: "change" }],
        typeId2: [{ required: true, message: "请选择调整等级", trigger: "change" }],
        repertoryAmount: [{ required: true, message: "请输入调整数量", trigger: "blur" },
        { validator: validatePass1, trigger: "blur" }
        ],
      },
      fromDate: {
      },
      value1: [],
      zhoucaiTypeList: [],
      zhoucaispecificationList: [],
      lowData: { checkAmount: '' },
      nameList: [],
      typeList: [],
      specificationList: [],
      dialog1: false,
      dialog2: false,
      dialog3: false,
    };
  },
  async created() {
    /*存储拷贝的原初始化fromDate数据*/
    this.tableParam = { ...this.tableParam, name: "", specificationName: "", type: '' };
    const { data } = await getDicsType({ codes: 'zhoucaiType,zhoucaispecification' })
    this.zhoucaiTypeList = data.zhoucaiType.items
    this.zhoucaispecificationList = data.zhoucaispecification.items
    this.gettableData();
  },
  methods: {
    /**弹窗打开或关闭*/
    async open(key) {
      this[key] = true
      if (key == "dialog3") {
        this.lowData = []
        this.queryList1([])
        return
      }
      if (key == 'dialog1' || key == 'dialog2') {
        if (key == 'dialog1') {
          this.lowData = {
            name: '',
            specificationId: '',
            typeId: ''
          }
        }
        if (key == 'dialog2') {
          this.lowData = {
            name: '',
            specificationId: '',
            typeId: '',
            typeId2: '',
          }
        }
        const { data } = await queryNameList()
        this.nameList = data
        this.typeList = []
        this.specificationList = []
        return
      }

    },
    reset(key) {
      this[key] = false
    },
    /**添加盘点记录*/
    async change(name) {
      const { data } = await querySpecification({ name: name, companyId: getUserInfo().companyId })
      this.specificationList = data
      this.lowData.specificationId = ''
      this.lowData.typeId = ''
      this.typeList = []
    },
    change1(low) {
      let a = this.specificationList.find((res) => (res.specificationId == low))
      this.lowData.specificationName = a.specificationName
      this.typeList = a.typeList
    },
    change2(low) {
      let a = this.typeList.find((res) => (res.typeId == low))
      this.lowData.type = a.type
      this.lowData.repertoryAmount = a.repertoryAmount
      this.lowData.repertoryManageId = a.id
    },
    submit1() {
      let _this = this;
      _this.$refs.dialog1.validate(async (valid) => {
        if (valid) {
          _this.lowData.companyId = await getUserInfo().companyId
          _this.lowData.company = await getUserInfo().companyName
          if (this.lowData.checkAmount == 0) {
            this.$confirm('是否确认当前盘点数量为0 ', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              saveOneResord(_this.lowData).then(res => {
                if (res.code == 1001) {
                  saveOneResord(_this.lowData).then(res => {
                    if (res.code == 1001) {
                      _this.$message({
                        message: '保存成功',
                        type: "success",
                      });
                      _this.gettableData();
                      _this.reset('dialog1')
                    }
                  })
                }
              })
            })
          }
        } else {
          return false;
        }
      });
    },
    /**周材调级*/
    async change3(name) {
      const { data } = await querySpecification({ name: name, companyId: getUserInfo().companyId })
      this.specificationList = data
      this.lowData.specificationId = ''
      this.lowData.typeId2 = ''
      this.lowData.repertoryAmount2 = ''
      this.typeList = []
    },
    change4(low) {
      let a = this.specificationList.find((res) => (res.specificationId == low))
      this.lowData.specificationName = a.specificationName
      this.typeList = a.typeList
    },
    change5(low) {
      let a = this.typeList.find((res) => (res.typeId == low))
      this.lowData.repertoryAmount2 = a.repertoryAmount
      this.lowData.id = a.id
    },
    submit2() {
      let _this = this;
      _this.$refs.dialog2.validate(async (valid) => {
        if (valid) {
          _this.lowData.companyId = await getUserInfo().companyId
          _this.lowData.company = await getUserInfo().companyName
          updateOrSave(_this.lowData).then(res => {
            if (res.code == 1001) {
              _this.$message({
                message: '保存成功',
                type: "success",
              });
              _this.reset('dialog2')
              _this.gettableData();
            }
          })
        } else {
          return false;
        }
      });
    },
    /**获取盘点记录*/
    async queryList1(time) {
      const { data } = await queryList({ createTime: time[0], updateTime: time[1] })
      this.lowData = data
    },

    /*重置数据,此方法必须存在，并且方发只重置条件*/
    init() {
      this.tableParam.name = "";
      this.tableParam.specificationName = "";
      this.tableParam.type = "";
      this.gettableData();
    },
    async pagesubmitFromDate() {
      this.fromDate.companyId = await getUserInfo().companyId
      this.fromDate.company = await getUserInfo().companyName
      this.submitFromDate();
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input,
.el-select,
.el-textarea {
  width: 240px;
}
/*
 <el-link class="table_link" :underline="false">查看配合比</el-link>*/
.table_link {
  margin: 0 10px;
  color: $bg_color;
  cursor: pointer;
}

.table_link:hover {
  opacity: 0.8;
}
</style>
